package com.kaoshi.user.controller;


import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.PutObjectResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.kaoshi.common.config.Result;
import com.kaoshi.user.config.AutoLog;
import com.kaoshi.user.config.UsernameHolder;
import com.kaoshi.user.pojo.Enter;
import com.kaoshi.user.service.EnterService;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.io.InputStream;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2024-10-29
 */
@RestController
@RequestMapping("/a/user")
public class EnterController {
    @Resource
    private EnterService enterService;

    @RequestMapping("/login")
    @AutoLog("登录")
    public Result login(@RequestBody Enter enter, HttpSession session) {
//        UsernameHolder.setUsername(enter.getUsername());
        System.out.println("____________________"+enter.getUsername()+"_______________________");
        Result login = enterService.login(enter);
        session.setAttribute("user", login.getData());
        return login;
    }

    @RequestMapping("/findPage")
    @AutoLog("查询用户")
    public Result findPage(@RequestBody Enter enter) {
        LambdaQueryWrapper<Enter> qr = new LambdaQueryWrapper<>();
        qr.eq(Enter::getId, enter.getId());
        Enter one = enterService.getOne(qr);
        UsernameHolder.setUsername(enter.getUsername());
        return Result.ok(one);
    }


    @RequestMapping("change")
    @AutoLog("修改用户")
    public Result change(@RequestBody Enter enter) {
        enterService.saveOrUpdate(enter);
        UsernameHolder.setUsername(enter.getUsername());
        return Result.ok();
    }




    // 重置密码并加密
//    @RequestMapping("reset")
//    public Result reset(@RequestBody Enter enter) {
//        System.out.println(enter);
//        LambdaQueryWrapper<Enter> qr = new LambdaQueryWrapper<>();
//        qr.eq(Enter::getId, enter.getId());
//        Enter one = enterService.getOne(qr);
//
//        if (one == null) {
//            return Result.fail("用户不存在");
//        }
//        if (!one.getPassword().equals(enter.getPassword())) {
//            return Result.fail("密码错误");
//        }
//        if (enter.getNewpassword1()==null || enter.getNewpassword2()==null){
//            return Result.fail("密码不能为空");
//        }
//        if (!enter.getNewpassword1().equals(enter.getNewpassword2())) {
//            return Result.fail("两次密码不一致");
//        }
//        one.setPassword(enter.getNewpassword1());
//        enterService.saveOrUpdate(one);
//
//        return Result.ok("密码修改成功");
//    }


    // 重置密码并加密
    @RequestMapping("reset")
    public Result reset(@RequestBody Enter enter) {
        LambdaQueryWrapper<Enter> qr = new LambdaQueryWrapper<>();
        qr.eq(Enter::getId, enter.getId());
        Enter one = enterService.getOne(qr);
        if (one == null) {
            return Result.fail("用户不存在");
        }
        // 创建 BCryptPasswordEncoder 实例
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();

        if (!passwordEncoder.matches(enter.getPassword(), one.getPassword())) {
            return Result.fail("密码错误");
        }
        if (enter.getNewpassword1() == null || enter.getNewpassword2() == null) {
            return Result.fail("密码不能为空");
        }
        if (!enter.getNewpassword1().equals(enter.getNewpassword2())) {
            return Result.fail("两次密码不一致");
        }

        // 使用 BCrypt 加密新密码
        String encodedPassword = passwordEncoder.encode(enter.getNewpassword1());
        one.setPassword(encodedPassword); // 设置加密后的密码

        enterService.saveOrUpdate(one);

        return Result.ok("密码修改成功");
    }


    @Value("${aliyun.endpoint}")
    private String endpoint;
    @Value("${aliyun.keyId}")
    private String keyid;
    @Value("${aliyun.keySecret}")
    private String keysecret;
    @Value("${aliyun.bucketName}")
    private String buncket;

    @RequestMapping("fileUpload")
    public String fileUpload(MultipartFile file) {
        String url="";
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, keyid, keysecret);
        try {
            // 创建PutObjectRequest对象。
            InputStream inputStream = file.getInputStream();
            String originalFilename = file.getOriginalFilename();

            String replace = UUID.randomUUID().toString().replace("-", "");
            originalFilename = replace+originalFilename;
            String time = new DateTime().toString("yyyy/MM/dd");
            originalFilename = time+"/"+originalFilename;

            PutObjectResult putObjectResult = ossClient.putObject(buncket, originalFilename, inputStream);

            url="https://"+buncket+"."+endpoint+"/"+originalFilename;
        } catch (Exception ce) {
            System.out.println("上传异常--");
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
        return url;
    }



}
